﻿
using nadModel.config;
using nadModel.displayBoard;
using PADWH3._5;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace PADWH35
{
    public partial class MoldChangingScheduleDialog : Form
    {

        private Timer colorTimer = null;
        private Timer tTimer = null;
        private MoldChangingScheduleEntityProcess proc = null;
        public MoldChangingScheduleEntityProcess GetMoldChangingScheduleEntityProcess()
        {
            if (proc == null) proc = new MoldChangingScheduleEntityProcess();
            return proc;
        }

        private MesSetupStatusEntityProcess statusProc = null;
        public MesSetupStatusEntityProcess GetMesSetupStatusEntityProcess()
        {
            if (statusProc == null) statusProc = new MesSetupStatusEntityProcess();
            return statusProc;
        }

        private Timer switchFormTimer = new Timer();

        public MoldChangingScheduleDialog()
        {

            InitializeComponent();

            tTimer = new Timer();
            tTimer.Tick += TTimer_Tick;


            dataGV.AutoGenerateColumns = false;
            setupStagesGV.AutoGenerateColumns = false;

            tTimer.Enabled = true;
            tTimer.Interval = 5000;


            switchFormTimer.Interval = 60000;
            switchFormTimer.Enabled = true;
            switchFormTimer.Tick += SwitchFormTimer_Tick;
        }

        private void SwitchFormTimer_Tick(object sender, EventArgs e)
        {
            if (tb.SelectedTab == tb1)
            {
                tb.SelectedTab = tb2;
                TTimer_Tick(null, null);
            }
            else
            {
                tb.SelectedTab = tb1;
                TTimer_Tick(null, null);
            }
        }

        private void TTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (tb.SelectedTab == tb1)
                {
                    MoldChangingScheduleEntity[] ents = GetMoldChangingScheduleEntityProcess().FindData(config.GetCompany());
                    dataGV.DataSource = ents;
                }
                else
                {
                    MesSetupStatusEntity[] ents = GetMesSetupStatusEntityProcess().FindData(config.GetCompany());
                    setupStagesGV.DataSource = ents;
                }

                ColorTimer_Tick(null, null);
            }
            catch (Exception ex)
            {

            }
        }

        private void ColorTimer_Tick(object sender, EventArgs e)
        {
            decimal d;
            for (int i = 0; i < dataGV.Rows.Count; i++)
            {
                if (!decimal.TryParse(dataGV.Rows[i].Cells[SetupStdCol.Index].Value.ToString(), out d) && decimal.Parse(dataGV.Rows[i].Cells[ActSetupTimeCol.Index].Value.ToString()) > 0)
                {
                    dataGV.Rows[i].Cells[ActSetupTimeCol.Index].Style.BackColor = Color.Red;
                }
                else if (d < decimal.Parse(dataGV.Rows[i].Cells[ActSetupTimeCol.Index].Value.ToString()))
                {
                    dataGV.Rows[i].Cells[ActSetupTimeCol.Index].Style.BackColor = Color.Red;
                }

                if (dataGV.Rows[i].Cells[typeCol.Index].Value.ToString() == "1")
                {
                    dataGV.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                }
                else if (dataGV.Rows[i].Cells[typeCol.Index].Value.ToString() == "0" && dataGV.Rows[i].Cells[TimeToGoCol.Index].Value.ToString() == "0")
                {
                    dataGV.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                }
                else
                {
                    dataGV.Rows[i].DefaultCellStyle.BackColor = Color.White;
                }
            }

            for (int i = 0; i < setupStagesGV.Rows.Count; i++)
            {
                if (decimal.Parse(setupStagesGV.Rows[i].Cells[mesST1SetupStdCol.Index].Value.ToString()) < decimal.Parse(setupStagesGV.Rows[i].Cells[mesST1ActualSetupProgressCol.Index].Value.ToString()))
                {
                    setupStagesGV.Rows[i].Cells[mesST1ActualSetupProgressCol.Index].Style.BackColor = Color.Red;
                }

                if (decimal.Parse(setupStagesGV.Rows[i].Cells[mesST2SetupStdCol.Index].Value.ToString()) < decimal.Parse(setupStagesGV.Rows[i].Cells[mesST2ActualSetupProgressCol.Index].Value.ToString()))
                {
                    setupStagesGV.Rows[i].Cells[mesST2ActualSetupProgressCol.Index].Style.BackColor = Color.Red;
                }

                if (decimal.Parse(setupStagesGV.Rows[i].Cells[mesST3SetupStdCol.Index].Value.ToString()) < decimal.Parse(setupStagesGV.Rows[i].Cells[mesST3ActualSetupProgressCol.Index].Value.ToString()))
                {
                    setupStagesGV.Rows[i].Cells[mesST3ActualSetupProgressCol.Index].Style.BackColor = Color.Red;
                }


                if (setupStagesGV.Rows[i].Cells[mesTypeCol.Index].Value.ToString() == "1")
                {
                    setupStagesGV.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                }
                else
                {
                    setupStagesGV.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                }

            }
        }
    }
}
